<?php
  include_once('./queries/utils.inc.php');

  function usageReport($connection, $index, $pn) {
    $sql = "SELECT userid, " .
           "hours, " . 
           "'(' || CAST (hours*100/(SELECT CAST(SUM(remotewallclocktime/3600) AS decimal(10, 1))" .
			       "FROM history_horizontal) AS decimal(6,2)) " .
           "|| '%' || ')' AS percentage ".
           "FROM " .
           "(SELECT owner AS userid, " .
	   "CAST(SUM(remotewallclocktime/3600) AS decimal(10, 1)) AS hours " .
           "FROM history_horizontal " .
           "GROUP BY owner) AS v ";

    $rcount = get_recordcount($connection, $sql);
    
    $sql = $sql . "ORDER BY  v.hours DESC " . get_limitclause($pn, $rcount);

    //echo $sql;
    unset($_SESSION["{$index}"]);
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"]="Condor usage report";        
    $_SESSION["{$index}"]["message"] = "Condor usage report";
    
    $fields = array();
    $fields[0] = "userid";
    $fields[1] = "hours";
    $fields[2] = "percentage";

    $rs = new recordset();
    $rs->__construct($rcount,
                   $fields,
                   get_recordset($connection, $sql));

    return $rs;
    
    //echo $sql;
  }

  function estimateTotalHours($connection) {
    $sql = "SELECT CAST(SUM(remotewallclocktime/3600) AS decimal(10,1)) " .
           "FROM history_horizontal;";
    
    return get_scalar(get_recordset($connection, $sql));

  }  
?>
